var regphone = /^1[3|4|5|7|8]\d{9}$/;
var regnum = /^([1-9][0-9]*|0)$/;
var postcode =  /^[1-9][0-9]{5}$/;

$(function () {
    var token = $("#token").val();
    if (isempty(token)) {
        location.href = "/except/login.jsp";
        return;
    }
    var addressid = $("#addressid").val();
    if (!isempty(addressid)) {
        var jsonstr = "{token:" + token + "}";
        var url = "/except/systemcontroller/getaddressInfo.action";
        $.ajax({
            url: url,
            type: 'post',
            dataType: "json",
            data: {
                json: jsonstr,
                addressid: addressid
            },
            async: false,
            success: function (data) {
                var code = data.code;
                var message = data.message;
                if (code == 10001 || code == 10002)
                    location.href = "/except/login.jsp";
                else if (code == 20000) {
                    $("#address_name").val(data.data.name);
                    $("#address_phone").val(data.data.phone);
                    $("#address").val(data.data.address);
                    $("#address_postcode").val(data.data.postcode);
                    var hidepid = $("#hide_pid").val();
                    var hidecid = $("#hide_cid").val();
                    var hidedid = $("#hide_did").val();
                    if(isempty(hidepid))
                        $("#hide_pid").val(data.data.pid);
                    if(isempty(hidecid))
                        $("#hide_cid").val(data.data.cid);
                    if(isempty(hidedid))
                        $("#hide_did").val(data.data.did);
                    var area = $("#area").html().replace(/(^\s*)|(\s*$)/g, "");
                    if (area == "选择") {
                        if(area.length > 12)
                            $("#area").html((data.data.provincename + data.data.cityname + data.data.districtname).substr(0, 12) + "...");
                        else
                            $("#area").html(data.data.provincename + data.data.cityname + data.data.districtname);

                    }
                    remove_advertising();
                } else {
                    $("#hint_message").html(message);
                    $("#hintbg").show();
                }
            }
        });
    }
});


function save() {
    var token = $("#token").val();
    if (isempty(token)) {
        location.href = "/except/login.jsp";
        return;
    }
    var address_name = $("#address_name").val();
    var address_phone = $("#address_phone").val();
    var address = $("#address").val();
    var address_postcode = $("#address_postcode").val();
    var pid = $("#hide_pid").val();
    var cid = $("#hide_cid").val();
    var did = $("#hide_did").val();
    if (isempty(address_name)) {
        $("#hint_message").html("请输入姓名");
        $("#hintbg").show();
        return;
    }
    if (isempty(address_phone)) {
        $("#hint_message").html("请输入联系方式");
        $("#hintbg").show();
        return;
    }else if (!regphone.test(address_phone)){
        $("#hint_message").html("请输入正确的联系方式");
        $("#hintbg").show();
        return;
    }
    if (isempty(address)) {
        $("#hint_message").html("请输入详细地址");
        $("#hintbg").show();
        return;
    }
    if (isempty(address_postcode)) {
        $("#hint_message").html("请输入邮编");
        $("#hintbg").show();
        return;
    } else if(!postcode.test(address_postcode)) {
        $("#hint_message").html("请输入正确的邮编（不能以0开头）");
        $("#hintbg").show();
        return;
    }
    if (isempty(pid) || isempty(cid)) {
        $("#hint_message").html("请选中省市区");
        $("#hintbg").show();
        return;
    }
    var jsonstr = "{token:" + token + "}";
    var addressid = $("#addressid").val();
    var url = "/except/systemcontroller/addaddress.action";
    if (isempty(addressid))
        addressid = "null";
    else
        url = "/except/systemcontroller/editaddressinfo.action";
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            json: jsonstr,
            name: address_name,
            phone: address_phone,
            pid: pid,
            cid: cid,
            did: did,
            address: address,
            postcode: address_postcode,
            addressid: addressid
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message;
            var addressinfo = "";
            if (code == 10001 || code == 10002)
                location.href = "/except/login.jsp";
            else if (code == 20000) {
                $("#address_add_frm").attr("action","/except/files/user/my_shop_address.jsp").submit();
            } else {
                $("#hint_message").html(message);
                $("#hintbg").show();
            }
        }
    });
}

function chosearea() {
    var hideupurl = $("#hideupurl").val();
    var upurl = $("#upurl").val();
    var url;
    if (!isempty(hideupurl))
        url = hideupurl;
    else
        url = upurl;
    $("#upurl").val(url);
    $("#address_add_frm").attr("action","/except/files/user/chose_city.jsp").submit();
}